<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机器名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入机器名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机器编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入机器编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:agentdevice:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:agentdevice:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="handleSpillover"
        >满溢设备</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handlebox"
        >箱子状态</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="handleExport"
        >生成机器二维码</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :border="true" v-loading="loading" :data="agentdeviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="模块" align="center" prop="module" />-->
      <el-table-column label="机器编号" align="center" prop="deviceCode" />
      <el-table-column label="机器名称" align="center" prop="deviceName" />
      <el-table-column label="机器地址" align="center"  />
      <el-table-column label="场地地址" align="center" prop="address" />
      <el-table-column label="场地所属" align="center"  prop="deviceName" />
      <el-table-column label="投放时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上次回收时间" align="center" width="180">
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.lastUpdateTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="设备类型" align="center"  />
      <el-table-column label="机器状态" align="center" prop="deviceStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.deviceStatus=='y'">正常</span>
          <span v-if="scope.row.deviceStatus=='n'">异常</span>
        </template>
      </el-table-column>
      <el-table-column label="在线状态" align="center"  >

      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:agentdevice:edit']"
          >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleDetails(scope.row)"
            >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:agentdevice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编号" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    <el-dialog title="满溢设备" :visible.sync="equipmentOpen" width="500px" append-to-body>-->
<!--      <el-table  :border="true" >-->
<!--        <el-table-column label="机器名称" align="center"  />-->
<!--        <el-table-column label="机器编码" align="center"  />-->
<!--        <el-table-column label="溢满度" align="center"  />-->
<!--        <el-table-column label="重量" align="center"  />-->
<!--        <el-table-column label="溢满时间" align="center"  />-->
<!--      </el-table>-->
<!--    </el-dialog>-->
     <!-- 满溢设备-->
    <equipment @closeFn="closeFn" :equipmentOpen="equipment" :titles="title2"></equipment>
    <!-- 箱子列表-->
    <equipment v-on:closeFn="closeFn" :equipmentOpen="equipmentBox" :titles="title2"></equipment>




  </div>
</template>

<script>
import { listAgentdevice, getAgentdevice, delAgentdevice, addAgentdevice, updateAgentdevice } from "@/api/module/agentdevice";
import equipment from "@/views/module/agentdevice/equipment";
export default {
  name: "Agentdevice",
  components:{
    equipment
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      agentdeviceList: [],
      // 弹出层标题
      title: "",
      title2:'1',
      // 是否显示弹出层
      open: false,
      equipment:false,
      equipmentBox:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        agentId: null,
        module: null,
        addressId: null,
        planId: null,
        deviceMac: null,
        deviceName: null,
        deviceCode: null,
        deviceStatus: null,
        contactPhone: null,
        longitude: null,
        latitude: null,
        appVersion: null,
        attrs: null,
        createTime: null,
        lastUpdateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        agentId: [
          { required: true, message: "运营商ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleDetails(value){
      this.$router.push({
        path:'module/agentdevice/agentdeviceDetails'
            //  path:"/home",
            // query:{
            //   id:this.id
            // }
    })
    },
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listAgentdevice(this.queryParams).then(response => {
        response.rows.forEach(item=>{
          if(item.street){
            item.address=item.province+item.city+item.district+item.street
          }else{
            item.address=item.province+item.city+item.district
          }
        })
        this.agentdeviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceId: null,
        agentId: null,
        module: null,
        addressId: null,
        planId: null,
        deviceMac: null,
        deviceName: null,
        deviceCode: null,
        deviceStatus: null,
        contactPhone: null,
        longitude: null,
        latitude: null,
        appVersion: null,
        attrs: null,
        remark: null,
        createTime: null,
        lastUpdateTime: null
      };
      this.resetForm("form");
    },
    /** 满溢设备 */
    handleSpillover(){
      this.title2=1
      this.equipment=true
    },
    closeFn(value){
      console.log(value)
      this.equipment=value
    },
    /** 满溢设备 */
    handlebox(){
      this.title2=0
      this.equipmentBox=true
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deviceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const deviceId = row.deviceId || this.ids
      getAgentdevice(deviceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.deviceId != null) {
            updateAgentdevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgentdevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const deviceIds = row.deviceId || this.ids;
      this.$modal.confirm('是否确认删除设备管理编号为"' + deviceIds + '"的数据项？').then(function() {
        return delAgentdevice(deviceIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('module/agentdevice/export', {
        ...this.queryParams
      }, `agentdevice_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
